package com.gj.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.gj.bean.UserBean;
import com.gj.dao.UserDao;
import com.gj.daoImp.UserDaoImple;

/**
 * Servlet implementation class doLoginServlet
 */
@WebServlet("/doLoginServlet")
public class doLoginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public doLoginServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=UTF-8");
		String userName=request.getParameter("username");
	    String password=request.getParameter("password");
		PrintWriter out=response.getWriter();
	    UserDao userMng = new UserDaoImple(); 
	    UserBean user = userMng.findUser(userName); 
	    if(!userName.isEmpty() && !password.isEmpty()){
	    	
	    	if(user!=null && user.getTelephone()!=null&& user.getPassword().equals(password)){  
	    		HttpSession session=request.getSession();
	    		 session.setAttribute("user", user); 
	    		   request.getRequestDispatcher("selectServlet").forward(request,response); 
	    		   }
	    		  else { 
	    		
	    		  out.println("<script>alert('用户名或密码错误,请重新输入!');history.back();</script>");  
	    		  out.flush();   
	    		  out.close();   
	    		  } 
	    }
	    out.println("<script>alert('用户名或密码不能为空!!');history.back();</script>");  
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
